package fr.cnam.projetSiw.client.structure.pages.suggest;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.cnam.projetSiw.client.core.ContextKeeper;
import fr.cnam.projetSiw.client.ressources.GeneralBundle;
import fr.cnam.projetSiw.client.structure.DescArtistPlace;
import fr.cnam.projetSiw.shared.Artist;
import fr.cnam.projetSiw.widget.PhotoSquare;

public class BornArtistViewImpl extends VerticalPanel implements BornArtistView
{

	private Label lblTitleDeb = new Label("Artistes");
	private Label lblTitleStrong = new Label(" né(e)s ");
	private Label lblTitleFin = new Label(" dans votre ville ");
	FlowPanel flow = new FlowPanel();

	public BornArtistViewImpl()
	{
		super();
		addStyleName(GeneralBundle.INSTANCE.css().tableSquare());
		lblTitleDeb.addStyleName(GeneralBundle.INSTANCE.css().titleSquare());
		lblTitleStrong.addStyleName(GeneralBundle.INSTANCE.css().titleSquare());
		lblTitleFin.addStyleName(GeneralBundle.INSTANCE.css().titleSquare());
		lblTitleStrong.addStyleName(GeneralBundle.INSTANCE.css().colorBlue());
		flow.addStyleName(GeneralBundle.INSTANCE.css().inline());
		flow.add(lblTitleDeb);
		flow.add(lblTitleStrong);
		flow.add(lblTitleFin);

		add(flow);
		this.setCellHeight(flow, "87px");
	}

	private Presenter presenter;

	@Override
	public void setPresenter(ZicPresenter presenter)
	{
		this.presenter = (Presenter) presenter;

	}

	@Override
	public void setData(ArrayList<Artist> result)
	{
		this.clear();
		this.add(flow);
		if (result == null || result.isEmpty() || result.get(0) == null)
			add(new Label("Aucun résultats !")) ;
		
		int cpt = 0;
		for (final Artist artist : result)
		{
			if (cpt == 6)
				break;
			// je jarte ceux qui vont me faire un rendu d��gueulasse
			if (artist.getImg() == null || artist.getImg().isEmpty() || artist.getGenres() == null || artist.getGenres().isEmpty())
				continue;
			
			// idem
			int lenght = 0, cptGenre = 0;
			for (int i = 0; i < artist.getGenres().size(); i++)
			{
				if (cptGenre == 2)
					break;
				artist.getGenres().set(i, artist.getGenres().get(i).trim());
				if(lenght + artist.getGenres().get(i).length() > 29 )
				{
					artist.getGenres().remove(i);
					i--;
				}
				else
				{
					lenght += artist.getGenres().get(i).length();
					cptGenre++;
				}
				
			}
			if (artist.getGenres().isEmpty())
			{
				continue;
			}

			PhotoSquare square = new PhotoSquare();

			String date = "Né(e) le " + DateTimeFormat.getFormat("dd MMMM yyyy").format(artist.getBirthDate());
			FocusPanel pnl = square.addSquareArtiste(artist.getImg(), artist.getName(), date, artist.getGenres());

			pnl.addClickHandler(new ClickHandler()
			{

				@Override
				public void onClick(ClickEvent event)
				{
					ContextKeeper.getInstance().setArtist(artist);
					presenter.goTo(new DescArtistPlace("DescArtistePlae"));
				}
			});

			add(square);
			cpt++;
		}
	}
}
